package com.inossem.warehousestorage_inossem.activity.query;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.query.extra.QueryExtra;
import com.inossem.warehousestorage_inossem.adapter.query.QueryOutStorageListAdapter;
import com.inossem.warehousestorage_inossem.bean.query.QueryOutStorageListBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.query.QueryOutStorageListRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.QueryUtils;

import java.util.List;

//出库查询列表
public class QueryOutStorageListActivity extends BaseActivity {

    private TextView title;

    private PullToRefreshLayout refresh;

    private PullableListView outStorageList;

    private List<QueryOutStorageListBean.DataBean.OutStorageListBean> allData;

    private QueryOutStorageListAdapter adapter;

    private String mInventoryAddressCode, mOutStorageNumber, mDateBegin, mDateEnd, mQueryOutStorageNumber;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_query_out_storage_list);
        initView(QueryOutStorageListActivity.this);
        initData(QueryOutStorageListActivity.this);
    }

    @Override
    protected void onResumeImpl() {

    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        outStorageList = (PullableListView) activity.findViewById(R.id.outStorageList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.outStorageListRefresh);
    }

    private void initData(Activity activity) {
        title.setText(getResources().getString(R.string.activity_query_out_storage_detail_outbound_list));
        ActivityUtils.initBackAndFunction(activity, getResources().getString(R.string.activity_query_manager_outbound_query), new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });

        outStorageList.setPullUpFlag(false);
        refresh.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                addOutStorageListData(activity, allData, outStorageList, true);
                pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
            }
        });
        getExtra(activity);
        getData(activity, mInventoryAddressCode, mOutStorageNumber, mDateBegin, mDateEnd);
//        setmScanListener((String scanResult, int length) -> {
//            mQueryOutStorageNumber = QRCodeUtils.DecryptQRCode(scanResult);
//            addOutStorageListData(activity, QueryUtils.filterOutStorageList(allData, mQueryOutStorageNumber), outStorageList, true);
//        }, QRCodeConstant.TYPE_BATCH, QRCodeConstant.TYPE_MATERIEL_CODE, QRCodeConstant.TYPE_PURCHASE_ORDER, QRCodeConstant.TYPE_POSITION);
    }

    private void getExtra(Activity activity) {
        mInventoryAddressCode = activity.getIntent().getStringExtra(QueryExtra.INVENTORY_ADDRESS_CODE);
        mOutStorageNumber = activity.getIntent().getStringExtra(QueryExtra.OUT_STORAGE_NUMBER);
        mDateBegin = activity.getIntent().getStringExtra(QueryExtra.DATE_BEGIN);
        mDateEnd = activity.getIntent().getStringExtra(QueryExtra.DATE_END);
    }

    private void addOutStorageListData(Context context, List<QueryOutStorageListBean.DataBean.OutStorageListBean> outStorageList, ListView listView, boolean flag) {
        if (flag) {
            adapter = new QueryOutStorageListAdapter(context, outStorageList, mQueryOutStorageNumber, (String queryOutStorageNumber) -> {
                mQueryOutStorageNumber = queryOutStorageNumber;
                addOutStorageListData(context, QueryUtils.filterOutStorageList(allData, mQueryOutStorageNumber), listView, true);
            });
            listView.setAdapter(adapter);
        }
    }

    private void getData(Context context, String inventoryAddressCode, String outStorageNumber, String dateBegin, String dateEnd) {
        QueryOutStorageListRequest.request(context, inventoryAddressCode, outStorageNumber, dateBegin, dateEnd, new GsonBeanListener<QueryOutStorageListBean>() {
            @Override
            public void onSuccess(QueryOutStorageListBean bean) {
                allData = bean.getData().getOutStorageList();
                if (allData == null || allData.isEmpty()) {
                    Utils.middleToast(context, getResources().getString(R.string.activity_in_storage_receive_list_no_message));
                }
                addOutStorageListData(context, ActivityUtils.getShowData(allData, 0), outStorageList, true);
            }
        });
    }

    public interface QueryOutStorageListListener {
        void onNoDoubleClickListener(String inStorageNumber);
    }

}
